﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Timers = System.Timers;
using System.Windows.Threading;

/// <summary>
/// 存放自定义控件
/// </summary>
namespace OpticalPowerMeter.UserControls
{

    /// <summary>
    /// 最多8个光功,Sample表示参考表，其他7个分别表示被测的7台表
    /// </summary>
    public enum MeterName
    {
        Sample = 0,
        One = 1,
        Two = 2,
        Three = 3,
        Four = 4,
        Five = 5,
        Six = 6,
        Seven = 7
    }

    //枚举连接状态，连接或断开连接
    public enum ConnectState
    {
        Connect,
        Disconnect
    }

    /// <summary>
    /// OpticalPowerMeterControl.xaml 的交互逻辑
    /// </summary>
    public partial class OpticalPowerMeterControl : UserControl
    {
        Timers.Timer flickerTimer = new Timers.Timer(1000); //1秒刷新显示定时器
        Dispatcher dispatch = App.Current.Dispatcher; //当前APP调度

        /// <summary>
        /// 无参构造器，初始化界面，并注册一个定时器事件，用于状态图标显示闪烁
        /// </summary>
        public OpticalPowerMeterControl()
        {
            InitializeComponent();
            flickerTimer.Elapsed += new Timers.ElapsedEventHandler(FlickerTestState);
        }


        //设备名称
        private static DependencyProperty MeterNameProperty = DependencyProperty.Register(
            "MeterName",
            typeof(MeterName),
            typeof(OpticalPowerMeterControl)
            );

        /// <summary>
        /// 机型名称属性，显示在控件左上角
        /// </summary>
        public MeterName MeterName
        {
            set { SetValue(MeterNameProperty, value); }
        }


        //是否连接
        private static DependencyProperty ConnectStateProperty = DependencyProperty.Register(
            "ConnectState",
            typeof(ConnectState),
            typeof(OpticalPowerMeterControl),
            new PropertyMetadata(ConnectState.Connect, new PropertyChangedCallback(OnConnectStateChanged)));

        /// <summary>
        /// 设置机型连接状态，显示在控件中下部
        /// </summary>
        public ConnectState ConnectState
        {
            get
            {
                ConnectState connect = ConnectState.Connect;
                try
                {
                    App.Current?.Dispatcher.Invoke(new Action(() =>
                    {
                        connect = (ConnectState)GetValue(ConnectStateProperty);
                    }));
                }
                catch { }
                return ConnectState;
            }

            set
            {
                App.Current?.Dispatcher.Invoke(new Action(() => {
                    SetValue(ConnectStateProperty, value);
                }));
            }
        }


        //波长
        public static DependencyProperty WavelengthProperty = DependencyProperty.Register(
            "Wavelength",
            typeof(String),
            typeof(OpticalPowerMeterControl)
            );

        public String Wavelength
        {
            get { return (String)GetValue(WavelengthProperty); }
            set
            {
                App.Current?.Dispatcher.Invoke(new Action(() => {
                    SetValue(WavelengthProperty, value);
                }));
            }
        }


        //功率
        public static DependencyProperty PowerProperty = DependencyProperty.Register(
            "Power",
            typeof(String),
            typeof(OpticalPowerMeterControl),
            new PropertyMetadata("Low")
            );

        public String Power
        {
            get { return (String)GetValue(PowerProperty); }
            set
            {
                App.Current?.Dispatcher.Invoke(new Action(() => {
                    SetValue(PowerProperty, value);
                }));
            }
        }


        //设备状态显示内容
        private static DependencyProperty MeterStatusProperty = DependencyProperty.Register(
            "MeterStatus",
            typeof(string),
            typeof(OpticalPowerMeterControl),
            new PropertyMetadata("", new PropertyChangedCallback(OnTestStatusChanged))
            );

        public string MeterStatus
        {
            set
            {
                App.Current?.Dispatcher.Invoke(new Action(() => {
                    SetValue(MeterStatusProperty, value);
                }));
            }
            get
            {
                string meterStatus = "";
                App.Current?.Dispatcher.Invoke(new Action(() => {
                    GetValue(MeterStatusProperty);
                }));
                return meterStatus;
            }
        }


        //设备状态颜色
        private static DependencyProperty MeterStatusBrushProperty = DependencyProperty.Register(
            "MeterStatusBrush",
            typeof(Brush),
            typeof(OpticalPowerMeterControl)
            );

        public Brush MeterStatusBrush
        {
            set
            {
                App.Current?.Dispatcher.Invoke(new Action(() => {
                    SetValue(MeterStatusBrushProperty, value);
                }));
            }
        }


        //连接状态发生变化时触发
        public static void OnConnectStateChanged(DependencyObject source,
            DependencyPropertyChangedEventArgs args)
        {
            var control = source as OpticalPowerMeterControl;
            control.SetColor();
        }

        //设置颜色,考虑是否在连接断开时将控件显示变暗，目前没有想到最贴切的表现方式，暂时这部分空着
        private void SetColor()
        {

        }


        //设备编号 圆形直径
        private static DependencyProperty CircleDiameterProperty = DependencyProperty.Register(
            "CircleDiameter",
            typeof(double),
            typeof(OpticalPowerMeterControl),
            new PropertyMetadata(20.0)
            );

        //设备名称Label字号
        private static DependencyProperty MeterNameFontSizeProperty = DependencyProperty.Register(
            "MeterNameFontSize",
            typeof(double),
            typeof(OpticalPowerMeterControl),
            new PropertyMetadata(14.0)
            );


        //设备名称位置
        private static DependencyProperty MeterNameMarginProperty = DependencyProperty.Register(
            "MeterNameMargin",
            typeof(Thickness),
            typeof(OpticalPowerMeterControl),
            new PropertyMetadata(new Thickness(6, 1, 0, 0))
            );


        //波长位置
        private static DependencyProperty WavelengthMarginProperty = DependencyProperty.Register(
            "WavelengthMargin",
            typeof(Thickness),
            typeof(OpticalPowerMeterControl)
            );


        //波长字体
        private static DependencyProperty WavelengthFontSizeProperty = DependencyProperty.Register(
            "WavelengthFontSize",
            typeof(double),
            typeof(OpticalPowerMeterControl)
            );

        //nm位置
        private static DependencyProperty NmLabelMarginProperty = DependencyProperty.Register(
            "NmLabelMargin",
            typeof(Thickness),
            typeof(OpticalPowerMeterControl)
            );

        //nm字号
        private static DependencyProperty NmLabelFontSizeProperty = DependencyProperty.Register(
            "NmLabelFontSize",
            typeof(double),
            typeof(OpticalPowerMeterControl)
            );


        //功率位置
        private static DependencyProperty PowerMarginProperty = DependencyProperty.Register(
            "PowerMargin",
            typeof(Thickness),
            typeof(OpticalPowerMeterControl)
            );


        //功率字号
        private static DependencyProperty PowerFontSizeProperty = DependencyProperty.Register(
            "PowerFontSize",
            typeof(double),
            typeof(OpticalPowerMeterControl)
            );

        //dBm位置
        private static DependencyProperty DbmLabelMarginProperty = DependencyProperty.Register(
            "DbmLabelMargin",
            typeof(Thickness),
            typeof(OpticalPowerMeterControl)
            );

        //dBm字号
        private static DependencyProperty DbmLabelFontSizeProperty = DependencyProperty.Register(
            "DbmLabelFontSize",
            typeof(double),
            typeof(OpticalPowerMeterControl)
        );


        //功率字号
        private static DependencyProperty TestResultFontSizeProperty = DependencyProperty.Register(
            "TestResultFontSize",
            typeof(double),
            typeof(OpticalPowerMeterControl)
            );

        //主控件尺寸发生变化时，调整相应控件的大小及位置
        private void ControlSizeChanged(object sender, SizeChangedEventArgs args)
        {
            double height = Math.Round(this.ActualHeight, 2);
            double width = Math.Round(this.ActualWidth, 2);
            SetValue(CircleDiameterProperty, Math.Round(height / 5, 2));  //修改圆圈的大小
            double circleDiameter = (double)GetValue(CircleDiameterProperty); //圆圈大小
            double circleFontSize = Math.Round((circleDiameter - 10) / 1.27 + 5.5, 1);
            //根据圆圈大小圆圈内字号
            SetValue(MeterNameFontSizeProperty, circleFontSize);

            //根据外侧圆圈的大小以及字的大小确定位置
            double labelHeight = Math.Round(1.27 * circleFontSize + 10, 2);
            double labelWidth = Math.Round(0.577 * circleFontSize + 10, 2);
            double meterNameLeft = Math.Round(5 + (circleDiameter - labelWidth) / 2, 2);
            double meterNameTop = Math.Round(5 + (circleDiameter - labelHeight) / 2, 2);
            Thickness meterNamethickness = new Thickness(meterNameLeft, meterNameTop, 0, 0);
            SetValue(MeterNameMarginProperty, meterNamethickness);

            //nm字样的边距及字体大小
            double nmLabelHeight = Math.Round(height / 8, 2);
            double nmLabelFontSize = Math.Round((nmLabelHeight - 10) / 1.27 + 5.5, 1);
            SetValue(NmLabelFontSizeProperty, nmLabelFontSize);
            double nmLabelLeft = Math.Round(width / 6 * 4, 2); //nm显示在控件的4/6位置
            double nmLabelTop = Math.Round(height / 3 - nmLabelHeight, 2); //显示在1/3处
            Thickness nmLabelThickness = new Thickness(nmLabelLeft, nmLabelTop, 0, 0);
            SetValue(NmLabelMarginProperty, nmLabelThickness);

            //dBm字样的边距及大小
            double dbmLabelHeight = Math.Round(height / 8, 2);
            double dBmLabelFontSize = Math.Round((dbmLabelHeight - 10) / 1.27 + 5.5, 1);
            SetValue(DbmLabelFontSizeProperty, dBmLabelFontSize);
            double dBmLabelLeft = Math.Round(width / 6 * 4, 2); //nm显示在控件的4/6位置
            double dBmLabelTop = Math.Round(height / 3 * 2 - dbmLabelHeight, 2); //显示在2/3处
            Thickness dBmLabelThickness = new Thickness(dBmLabelLeft, dBmLabelTop, 0, 0);
            SetValue(DbmLabelMarginProperty, dBmLabelThickness);

            //波长边距及大小
            double wavelengthLabelHeight = Math.Round(height / 4, 2);
            double wavellengthLabelFontSize = Math.Round((wavelengthLabelHeight - 10) / 1.27 + 5.5, 1);
            SetValue(WavelengthFontSizeProperty, wavellengthLabelFontSize);
            double wavelengthLabelLeft = Math.Round(width / 6 * 2, 2); //波长显示在控件的2/6位置
            double wavelengthLabelTop = Math.Round(height / 3 - wavelengthLabelHeight, 2); //显示在1/3处
            Thickness wavelengthLabelThickness = new Thickness(wavelengthLabelLeft, wavelengthLabelTop, 0, 0);
            SetValue(WavelengthMarginProperty, wavelengthLabelThickness);

            //功率边距及大小
            double powerLabelHeight = Math.Round(height / 4, 2);
            double powerLabelFontSize = Math.Round((powerLabelHeight - 10) / 1.27 + 5.5, 1);
            SetValue(PowerFontSizeProperty, powerLabelFontSize);
            double powerLabelLeft = Math.Round(width / 6 * 2, 2); //显示在控件的2/6位置
            double powerLabelTop = Math.Round(height / 3 * 2 - wavelengthLabelHeight, 2); //显示在2/3处
            Thickness powerLabelThickness = new Thickness(powerLabelLeft, powerLabelTop, 0, 0);
            SetValue(PowerMarginProperty, powerLabelThickness);

            //测试结果字号
            double testResultLabelHeight = Math.Round(height / 4, 2);
            double testResultLabelFontSize = Math.Round((testResultLabelHeight - 10) / 1.27 + 5.5, 1);
            SetValue(TestResultFontSizeProperty, testResultLabelFontSize);
        }


        //测试状态发生变化时触发,状态需要有比较好的表现方式表示过程中、pass、fail
        public static void OnTestStatusChanged(DependencyObject source,
            DependencyPropertyChangedEventArgs args)
        {
            var control = source as OpticalPowerMeterControl;
            switch (control.MeterStatus)
            {
                case "":
                case "准备中":
                    control.flickerTimer.Start();
                    break;
                default:
                    control.flickerTimer.Stop();
                    break;
            }
        }


        //闪烁状态
        private void FlickerTestState(Object obj, Timers.ElapsedEventArgs e)
        {
            Visibility visibility = TestStateLabel.Visibility;
            try
            {
                if (visibility == Visibility.Hidden)
                {
                    dispatch.Invoke(new Action(() =>
                    {
                        TestStateLabel.Visibility = Visibility.Visible;
                    }));

                }
                else if (visibility == Visibility.Visible)
                {
                    dispatch.Invoke(new Action(() =>
                    {
                        TestStateLabel.Visibility = Visibility.Hidden;
                    }));

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
